import 'dart:math';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:smart_devices/model/spare_info.dart';
import 'package:smart_devices/model/spare_part.dart';

class SpareInfoLogic extends GetxController {
  RxInt controlIndex = RxInt(0);

  Rx<SparePartModel> details = Rx(SparePartModel(
      imgUrl: '',
      name: '',
      code: '',
      model: '',
      count: 0,
      change: 0,
      inventory: 0));

  RxList<XFile> imgs = RxList([]);

  RxList<PlatformFile> files = RxList([]);

  RxList<SpareInfoModel> pageList = RxList([]);

  PageController pageController = PageController();

  late RefreshController refreshController;

  @override
  void onInit() {
    super.onInit();
    if (Get.arguments != null) {
      details.value = Get.arguments as SparePartModel;
    }
    var r = Random();
    for (var i = 0; i < 8; i++) {
      imgs.add(XFile(
        'https://img1.baidu.com/it/u=3285409119,3504542108&fm=253&fmt=auto&app=120&f=JPEG?w=1063&h=709',
        length: r.nextInt(100000),
      ));
    }
    for (var i = 0; i < 10; i++) {
      pageList.add(
          SpareInfoModel(name: '仓库一', location: 'WZ057985', inventory: 50));
    }
    files.add(PlatformFile(name: '文档.pdf', size: 25678));
    files.add(PlatformFile(name: '表格文档.xlsx', size: 25678));
    files.add(PlatformFile(name: '幻灯片文档.pptx', size: 25678));
    files.add(PlatformFile(name: '图片.png', size: 25678));
    files.add(PlatformFile(name: 'Word文档.docx', size: 25678));
    files.add(PlatformFile(name: '压缩文档.zip', size: 25678));
    refreshController = RefreshController(initialRefresh: false);
  }

  @override
  void onClose() {
    super.onClose();
    pageController.dispose();
  }

  Future<void> onRefresh() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));
    refreshController.refreshCompleted(resetFooterState: true);
  }

  Future<void> onLoading() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));

    /// if failed, use loadFailed(), if no data return, use LoadNodata()
    if (pageList.length > 15) {
      return refreshController.loadNoData();
    }

    /// 添加
    refreshController.loadComplete();
    pageList
        .add(SpareInfoModel(name: '仓库一', location: 'WZ0127812', inventory: 10));
  }
}
